package com.study.guide.pack.splicing.DelimiterBasedFrameDecoder;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

public class TimeClientHandler extends SimpleChannelInboundHandler<Object> {

	private int counter;
	static final String ECHO_REQ ="Hi Lilinfeng. Wecome to Netty .$_";


	public TimeClientHandler() {

	}
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
		String body = (String)msg;
		System.out.println("this is : "+ ++counter +" timer receive server : ["+ body+"]");
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {

		for(int i=0;i<100;i++) {
			ByteBuf message =Unpooled.copiedBuffer(ECHO_REQ.getBytes());
			ctx.writeAndFlush(message);
		}
	}



	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

		ctx.close();
	}
}
